﻿var month_names = new Array();
month_names[month_names.length] = ".janúar";
month_names[month_names.length] = ".febrúar";
month_names[month_names.length] = ".mars";
month_names[month_names.length] = ".apríl";
month_names[month_names.length] = ".maí";
month_names[month_names.length] = ".júní";
month_names[month_names.length] = ".júlí";
month_names[month_names.length] = ".ágúst";
month_names[month_names.length] = ".september";
month_names[month_names.length] = ".oktober";
month_names[month_names.length] = ".nóvember";
month_names[month_names.length] = ".desember";

function ConvertStringToJSDate(dt) {
    var dtE = /^\/Date\((-?[0-9]+)\)\/$/.exec(dt);
    if (dtE) {
        var tmp = new Date(parseInt(dtE[1], 10));
        var hours = tmp.getHours();
        var minutes = tmp.getMinutes();
        if (hours < 10) {
            hours = "0" + hours;
        }
        if (minutes < 10) {
            minutes = "0" + minutes;
        }
        var value = tmp.getDate() + " " + month_names[tmp.getMonth()] + " kl " + hours + ":" + minutes;
        return value;
    }
    return null;
}

function saveLike(id) {
    $.post("/Video/Like/" + id, "", function (data) {
        getLike(id);
    });
    return false;
}
function getLike(id) {
    $.get("/Video/GetAllLikes/" + id, "", function (data) {
        var likes = "";
        for (var i = 0; i < data.length; i++) {
            likes += data[i].User;
            if (i != data.length - 1) {
                likes += "\n";
            }
        }
        if (data.length > 0) {
            $("ul li #" + id).empty();
            $("#" + id).append(" " + data.length + " Líkar Þetta");
        }
        else {
            $("#" + id).append("Engum Líkar Þetta");
        }

        $("#" + id).attr("title", likes);
    });
}


$(document).ready(function () {
    $(function () {
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=uft-8",
            url: "/Video/GetAllComments/",
            data: { "id": VideoId },
            dataType: "json",
            success: function (comments) {
                var tooltip = "";
                for (var i = 0; i < comments.length; i++) {
                    comments[i].CommentDate = ConvertStringToJSDate(comments[i].CommentDate);
                    getLike(comments[i].ID);
                }
                $("#commentTemplate").tmpl(comments).prependTo("#commentList");

            },
            error: function (xhr, err) {
                alert("readyState: " + xhr.readyState +
              "\nstatus: " + xhr.status);
                alert("responseText: " + xhr.responseText);
            }
        });
    });

    $("#btnSubmit").click(function () {
        var videoId = $("#videoId").val();
        var comment = { "Content": $("#CommentText").val(), "VideoId": videoId };
        $.post("/Video/AddComment", comment, function (comments) {
            $("#commentText").val("");
            //for (var i = 0; i < comments.length; i++) {
                //comments[i].CommentDate = ConvertStringToJSDate(comments[i].CommentDate);
                //getLike(comments[i].ID);
            //}
            $("#CommentText").val("");
            $("ul li:not(:first):not(:last)").remove();
            $("#commentTemplate").tmpl(comments).prependTo("#commentList");
               
        });

    });
});


$(function () {
    $(".tabbed").tabs();
});

$(function () {
    $(".gamelevellist").hide();
    $(".gamedescription").show();
    $(".gamepoints").hide();
});

$(function () {
    $("#showlvl").click(function() {
        $(".gamelevellist").show();
        $(".gamedescription").hide();
        $(".gamepoints").hide();
        });
});

$(function () {
    $("#showdes").click(function() {
        $(".gamelevellist").hide();
        $(".gamedescription").show();
        $(".gamepoints").hide();
    });
});

$(function () {
    $("#showpoint").click(function () {
        $(".gamelevellist").hide();
        $(".gamedescription").hide();
        $(".gamepoints").show();
    });
});